<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp" %>

<!DOCTYPE html>
<html>
<head>
    <title><spring:message code="message.title.center_jueseManage"></spring:message></title>
    <%@ include file="/WEB-INF/component/commonCSS.jsp" %>
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
							
    <section class="content-header">
        <h1><spring:message code="message.title.center_jueseManage"></spring:message></h1>
        <ol class="breadcrumb">
            <li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"></spring:message></a></li>
            <li class="active"><a href="#">用户管理</a></li>
            <li class="active"><a href="#">用户角色管理</a></li>
        </ol>
    </section>
    
    <section class="content">
        
        <table class="jqgrid" id="gridlist"></table>
        <div id="gridpage"></div>
		<div class=" box box-warning bottongroup">
			<label id="addNew" onclick="savedata()" class="btn btn-default btn-sm">保 存</label>
			<label id="batchUpdate" onclick="javaScript:window.location.href='${pageContext.request.contextPath}/sysUser/zxUserList.html'" class="btn btn-default btn-sm">返 回</label>

		</div>
    </section>
    
    <%@ include file="/WEB-INF/component/commonJS.jsp" %>
    
    <script type="text/javascript">
    function savedata(){
    	var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
    	var list = [];
    	for(var i = 0;i<rowIds.length;i++){
			list.push({"id":rowIds[i]});
		}
    	$.HN.message.confirm("确定要保存角色？", '', '').on(function (e) {
            if (e) {
		$.post("${pageContext.request.contextPath}/sysUser/saveUserRole.html", {
			"array" : json.jsonToString(list),
			"userId":'${userId}'
		}, function(result) {
			if (result.success) {
				$.alert("<spring:message code="message.HN.alert.chengGongCaoZuo"/>"+list.length+"<spring:message code="message.HN.alert.tiaoShuJu"/>", "success");
				$("#gridlist").trigger("reloadGrid");
			} else {
				$.alert(result.msg || "<spring:message code="message.HN.alert.fail"/>", "error");
			}
		}, "json");
            }
		});
    }
    
    
    
    
    
    function deleteRole(id,name){
    	$.HN.message.confirm("确定要删除<b>"+name+"</b>角色？", '', '').on(function (e) {
             if (e) {
            	 $.ajax({
                     url: "${pageContext.request.contextPath}/sysRole/deleteRole.html",//action地址
                     data: { "id":id},//传值
                     type: "post",//执行方式get or post 默认以post方式执行
                     dataType:"json",
                     success: function (result) {//执行成功后 提示
                         if (result.success){
                        	 top.$.HN.message.alert("<spring:message code='message.HN.alert.success'/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code='message.HN.alert.type'/>", "success");
                     	   gridSerch($("#gridlist").jqGrid('getGridParam','page'));
                        } else {
                        	$.HN.message.alert("删除失败！", "<spring:message code='message.HN.alert.type'/>", "error");
                        }
                     }
                 });
   		}
      	  });
    }
    
    function addRole(){
		 if ($.trim($("#rolename").val()).length < 1){
			 $.HN.message.alert("角色名称不能为空！", "warn");
			 return;
		 }
		 $.HN.message.confirm("确定要新增角色？", '', '').on(function (e) {
            if (e) {
         		$("#addForm").ajaxSubmit({
      			dataType : "json",
      			timeout: 10000,
      			success : function(result, statusText) {
      				if (result.success) {
      					    $('#addRoleModal').modal('hide');
      					    search( $("#gridlist").getGridParam("page"));
      					    top.$.HN.message.alert("<spring:message code='message.HN.alert.success'/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code='message.HN.alert.type'/>", "success");
      				} else {
      					top.$.HN.message.alert(result.msg || "", "<spring:message code='message.HN.alert.type'/>", "error");
      				}
      			}
      		});
  		}
     	  });
	 }
    
    
    
    
    	function search(page) {
    		var name = $.trim($("#name").val());
    		$("#gridlist").jqGrid('setGridParam',{  
    	        datatype:'json',
    	        postData:{
    	        	"name":name
    	        },
    	        page : (page || 1)
    	    }).trigger("reloadGrid"); //重新载入  
    	}
    	
    	function mainRolePrivilege(id,name){
    		$.HN.dialog.open({
	    		id:id,
	    		title:name,
	    		url:"${ctx}/sysPrivilege/mainRolePrivilege.html?roleId="+id,
	    		width:"800px",
	    		height:"550px",
	    		closefunc:function(){
	    		    // search();
	    		}
    		});
    	}
    	
    	// 临时用，发布时注释掉主角色的权限管理功能
    	function mainRolePrivilegeTemp(id,name){
            $.HN.dialog.open({
                id:id,
                title:name,
                url:"${ctx}/sysPrivilege/mainRolePrivilegeTemp.html?roleId="+id,
                width:"800px",
                height:"550px",
                closefunc:function(){
                    // search();
                }
            });
        }
    
        $(function () {
            $("#gridlist").jqGrid({
                url: "${pageContext.request.contextPath}/sysRole/getUserRoleList.html?userId=${userId}",
                mtype: "post",
                postData:{"main":1, "pid":"0"},
                datatype: "json",
                autowidth: true,
                height: 340,
                colNames: [
					'<spring:message code="message.lable.jueseName"></spring:message>', 
					'<spring:message code="message.lable.remark"></spring:message>' ,
					'chk'
                ],
                colModel: [
				   
                    { name: 'name', width: 10, align:"center", sortable: false},
                    { name: 'remark', width: 14, align:"center", sortable: false},
                    { name: 'role_id', width: 14, align:"center",hidden:true, sortable: false}
                    
                ],
                rowNum: 0,
                rownumbers: true,
                multiselect: true,
                viewrecords: true,
                caption: "用户角色管理",
                gridComplete:function(){
                    	var ids = jQuery("#gridlist").jqGrid('getDataIDs');
                    	for(var i=0;i < ids.length;i++){
                    		var rowdata = $("#gridlist").jqGrid('getRowData', ids[i]);
                    		if (rowdata.role_id.length > 0){
                    			$("#jqg_gridlist_"+ids[i]).attr('checked',true);
                    		}
                    	}
                },
                jsonReader: {
                    id: "id"
                }
            });
            
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            
            $(window).trigger("resize");
            
        });
    </script>
</body>
</html>












